// pages/my/myOrder/myOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIndex:0,
    orderList:null,
    countPage:0,
    countSwitch:true,
    waitPage:0,
    waitSwitch:true,
    unfinishPage:0,
    unfinishSwitch:true,
    finishPage:0,
    finishSwitch:true,
    userMoney:null,
    isPullDownCount:false,
    isPullDownWait:false,
    isPullDownunfinish:false,
    isPullDownFinish:false,
    money: 0,
    compare: app.globalData.min_money,
    isShowCode:false,
    codeImg:''
  },
  concat(e) {
    let _this = this,
        id = e.currentTarget.dataset.id;
    app.getBalance(function(money){
      app.util.ajax({
        url:'api/Appointment/contact',
        data:{
          id:id
        }
      },function(res){
        let min_money = Number(res.data.data.limit_money),
            user_money = Number(money);
        console.log(res.data.data.qrcode);
        if (money >= min_money) {
          _this.setData({
            isShowCode:true,
            codeImg:res.data.data.qrcode
          })
        }else{
          app.showServiceTips(min_money);
        }
      })
    })
  },
  onLoad: function (options) {
    // this.money();
    if (options.navIndex) {
      this.setData({
        navIndex:options.navIndex
      })
    };
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    // _this.money();
    this.resetSwtich();
    this.getOrderList(this.data.navIndex);
  },

  onPullDownRefresh: function () {
    this.resetSwtich();
    this.getOrderList(this.data.navIndex);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrderList(this.data.navIndex);
  },

  // 更换导航
  changeNav:function(event){
    let index = event.currentTarget.dataset.index;
    this.setData({
      navIndex:index
    });
    this.resetSwtich();
    this.getOrderList(this.data.navIndex);
  },
  // 请求不同订单
  getOrderList:function(type,pullDownBool){
    let orderType = '',
        typePage = 0,
        requestSwtich = true,
        isPullDown = false,
        _this = this;
    switch(type){
      case "1":
        orderType = 'WAITPAY';
        typePage = _this.data.waitPage;
        requestSwtich = _this.data.waitSwitch;
        break;
      case "2":
        orderType = 'WAITSEND';
        typePage = _this.data.unfinishPage;
        requestSwtich = _this.data.unfinishSwitch;
        break;
      case "3":
        orderType = 'WAITCCOMMENT';
        typePage = _this.data.finishPage;
        requestSwtich = _this.data.finishSwitch;
        break;
      default : 
        orderType = '';
        typePage = _this.data.countPage;
        requestSwtich = _this.data.countSwitch;
        break;
    }
    if (requestSwtich) {
      typePage++;
      app.util.ajax({
        url:'api/Order/orderList',
        data:{
          type:orderType,
          page:typePage
        }
      },function(res){
        wx.stopPullDownRefresh();
        let data = res.data.data,
            dataList = data.list ? data.list : data,
            switchType = '',
            page = null,
            setData = {
              orderList:dataList
            };
        if (Array.isArray(data)) {
          if (type == '0') {switchType = 'countSwitch'};
          if (type == '1') {switchType = 'waitSwitch'};
          if (type == '2') {switchType = 'unfinishSwitch'};
          if (type == '3') {switchType = 'finishSwitch'};
          setData[switchType] = false;
        }else{
          if (type == '0') {page = 'countPage'};
          if (type == '1') {page = 'waitPage'};
          if (type == '2') {page = 'unfinishPage'};
          if (type == '3') {page = 'finishPage'};
          setData[page] = typePage;
        }
        if (typePage > 1) {
          setData.orderList = _this.data.orderList.concat(setData.orderList);
        }
        _this.setData(setData);
      })
    }
  },
  // 跳转订单完成
  jumpOrderFinish:function(e){
    let id = e.currentTarget.dataset.id;
    app.util.pageJump('/pages/my/finishOrder/finishOrder?orderId='+id);
  },
  // 跳转未完成订单
  jumpNotFinish:function(e){
    let id = e.currentTarget.dataset.id;
    app.util.pageJump('/pages/my/unfinishOrder/unfinishOrder?orderId='+id);
  },
  // 确认服务
  comfirmServer:function(e){
    let id = e.currentTarget.dataset.id,
        _this = this;
    app.util.ajax({
      url:"api/Order/orderConfirm",
      data:{
        order_id:id
      }
    },function(data){
      _this.resetSwtich();
      _this.getOrderList(_this.data.navIndex);
    })
  },
  // 控制重置
  resetSwtich:function(){
    this.setData({
        countPage:0,
        waitPage:0,
        unfinishPage:0,
        finishPage:0,
        unfinishSwitch:true,
        finishSwitch:true,
        countSwitch:true,
        waitSwitch:true
      });
  },
  // 提交评价
  submitEvaluate:function(e){
    let index = Number(e.currentTarget.dataset.index),
        orderId = this.data.orderList[index].order_id,
        appointmentId = this.data.orderList[index].order_appointment.appointment_id,
        str = 'orderId='+orderId+"&appointmentId="+appointmentId;
    app.util.pageJump('/pages/my/star/star?'+str);
  },
  // 立即支付
  atoncePayment:function(e){
    let index = e.currentTarget.dataset.index,
        _this = this,
        orderId = this.data.orderList[index].order_id,
        orderSn = this.data.orderList[index].order_sn,
        orderPrice = this.data.orderList[index].service_amount;
    app.util.pageJump('/pages/index/detail/placeOrder/paymentPassword/paymentPassword?order_id='+orderId+'&order_sn='+orderSn+'&price='+orderPrice);
  }
})